package com.moonciki.artadmin.core.enums;

import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

/**
 * @author 岳仕奇
 * @description: TODO
 * @date 2022/1/19 13:43
 */
public enum FileTypeEnum {

    bmp(".bmp", "image/*"),
    gif(".gif", "image/*"),
    jpeg(".jpeg", "image/*"),
    jpg(".jpg", "image/*"),
    png(".png", "image/*"),

    xls(".xls", "application/vnd.ms-excel"),
    xlsx(".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"),

    doc(".doc", "application/msword"),
    docx(".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"),

    ppt(".ppt", "application/vnd.ms-powerpoint"),
    pptx(".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"),

    pdf(".pdf", "application/pdf"),
    mdb(".mdb", "application/octet-stream"),

    other("", "application/octet-stream"),

    ;

    @Getter
    private String suffix;

    @Getter
    private String contentType;

    private FileTypeEnum(String suffix, String contentType) {
        this.suffix = suffix;
        this.contentType = contentType;
    }


    public static String getContentType(String fileName){
        FileTypeEnum resultEnum = FileTypeEnum.other;

        int index = fileName.lastIndexOf(".");

        String suffixTmp = null;

        if(index >= 0){
            suffixTmp = fileName.substring(index).toLowerCase();
        }

        if(StringUtils.isNotBlank(suffixTmp)){

            for (FileTypeEnum oneEnum : FileTypeEnum.values()) {
                String oneSuffix = oneEnum.getSuffix();

                if(suffixTmp.equalsIgnoreCase(oneSuffix)){
                    resultEnum = oneEnum;
                    break;
                }
            }

        }

        String contentType = resultEnum.getContentType();
        return contentType;
    }

}
